<template>
  <!--
  重点:
    1. 各路数据的显示与不显示( isDeleteShow )
      |-1. 点击垃圾桶图标显示文字及删除图标,点击完成隐藏文字及删除图标,并显示垃圾桶图标
    2. 正常状态下点击历史记录列表触发搜索操作
    3. 删除状态下点击历史记录列表触发删除操作
    4. 全部删除时不能直接操作数据
-->
  <div class="search-history">
    <van-cell title="搜索历史">
      <template v-if="isDeleteShow">
        <span @click="$emit('clear-search-history')">全部删除</span>
        &nbsp;&nbsp;
        <span @click="isDeleteShow = false">完成</span>
      </template>
      <van-icon name="delete-o" v-else @click="isDeleteShow = true" />
    </van-cell>
    <van-cell :title="item" @click="onClickItem(item,index)" clickable v-for="(item, index) in searchHistory" :key="index">
      <van-icon name="close" v-show="isDeleteShow" />
    </van-cell>
  </div>
</template>

<script>
export default {
  name: 'SearchHistory',
  components: {

  },
  props: {
    searchHistory: {
      type: Array,
      required: true
    }
  },
  data () {
    return {
      isDeleteShow: false
    }
  },
  created () {

  },
  methods: {
    onClickItem (item, index) {
      // 判断状态
      if (this.isDeleteShow) {
        // 删除状态
        this.searchHistory.splice(index, 1)
      } else {
        // 正常状态
        this.$emit('search', item)
      }
    }
  }
}
</script>

<style lang="less" scoped>
</style>
